package company.shopee_F;

import java.util.Scanner;

public class Shopee03A {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int[][] array = new int[size][size];
        for (int i = 0;i<size;i++){
            for (int k = 0;k<size;k++){
                array[i][k] = scanner.nextInt();
            }
        }
        System.out.print(getMinRoad(array,size));

    }

    public static  int getMinRoad(int[][] array,int size){

        int count = 0;
        double[][] lengthArray = new double[size][size];
        for (int i = 0;i<size;i++) {
            for (int k = 0; k < size; k++) {
                if (array[i][k] == 1) {
                    count++;
                }
            }
        }
        if (count==size*size){
            return -1;
        }else {
            for (int j = 0;j<size;j++){
                for(int k = 0;k<size;k++){
                    if (array[j][k]==1){
                        lengthArray[j][k] = -1;
                    }else {
                        for (int m = 0;m<size;m++){
                            for (int n = 0;n<size;n++){
                                if (array[m][n]==1){
                                    // lengthArray[j][k]+=(int)Math.sqrt((m-j)*(m-j)+(n-k)*(n-k));
                                    lengthArray[j][k] +=(int)(Math.abs(m-j)+Math.abs(n-k));
                                }

                            }
                        }
                    }
                }
            }
        }
        double minLength = 0;
        for (int i = 0;i<size;i++){
            for (int j = 0;j<size;j++){
                if (lengthArray[i][j]>0){
                    minLength=lengthArray[i][j];
                    break;
                }
            }
            break;
        }

        for (int i = 0;i<size;i++){
            for (int j = 0;j<size;j++){
                if (lengthArray[i][j]>0){
                    if (minLength>lengthArray[i][j]){
                        minLength = lengthArray[i][j];
                    }
                }
            }
        }

        return (int)minLength ;
    }
}
